<?php

    /**
     * Zend Mail
     *
     * @package      Core
     * @subpackage   Mail
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Core_Mail_Zend extends Core_Mail_Abstract
    {

    ////////////////////////////////////////////////////////////////////////////
    //                          Attributes                                    //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Zend Mail instance
         *
         * @var     Zend_Mail
         */
        private $_instance;

    ////////////////////////////////////////////////////////////////////////////
    //                          Public Methods                                //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Constructor
         *
         * @param       string      $end
         */
        public function __construct($end = 'frontend')
        {
            parent::__construct($end);

            $this->_instance = new Zend_Mail('UTF-8');
        }

        /**
         * Add to
         *
         * @param       string      $email
         * @param       string      $name
         * @return      void
         */
        public function addTo($email, $name = '')
        {
            $this->_instance->addTo($email, $name);
        }

        /**
         * Add cc
         *
         * @param       string      $email
         * @param       string      $name
         * @return      void
         */
        public function addCc($email, $name = '')
        {
            $this->_instance->addCc($email, $name);
        }

        /**
         * Add bcc
         *
         * @param       string      $email
         * @return      void
         */
        public function addBcc($email)
        {
            $this->_instance->addBcc($email);
        }

        /**
         * Adds attachment
         *
         * @param       Zend_Mime_Part      $attachment
         */
        public function addAttachment(Zend_Mime_Part $attachment)
        {
            $this->_instance->addAttachment($attachment);
        }

        /**
         * Set from
         *
         * @param       string      $email
         * @param       string      $name
         * @return      void
         */
        public function setFrom($email, $name = '')
        {
            $this->_instance->setFrom($email, $name);
        }

        /**
         * Set subject
         *
         * @param       string      $subject
         * @return      void
         */
        public function setSubject($subject)
        {
            $this->_instance->setSubject(DbConfiguration::getValue('SITE_NAME').' - '.$subject);
        }

        /**
         * Send
         *
         * @return      void
         */
        public function send()
        {
            if ($this->_instance->getFrom() == '') {
                $this->_instance->setFrom(DbConfiguration::getValue('DEFAULT_EMAIL_SENDER'), DbConfiguration::getValue('SITE_NAME'));
            }

            $dwoo              = new Dwoo(Core_IO_Path::combine(PROJECT_TEMP_TEMPLATES, 'compiled'), Core_IO_Path::combine(PROJECT_TEMP_TEMPLATES, 'cache'));
            $dwooData          = new Dwoo_Data();

            foreach ($this->getParams() as $key => $value) {
                $dwooData->assign($key, $value);
            }

            $bodyHtml = $dwoo->get($this->getTemplate('html'), $dwooData);
            $bodyText = $dwoo->get($this->getTemplate('text'), $dwooData);

            $this->_instance->setBodyHtml($bodyHtml, 'UTF-8', Zend_Mime::ENCODING_8BIT);

            // Don't send HTML and Text version otherwise MIME encoding fails !
			// $this->_instance->setBodyText($bodyText, 'UTF-8', Zend_Mime::ENCODING_8BIT);

            $transport = new Zend_Mail_Transport_Smtp(DbConfiguration::getValue('MAIL_HOST'));

            $this->_instance->send($transport);
        }
    }